package com.sigmazero13.MKDummy.activites;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.sigmazero13.MKDummy.R;
import com.sigmazero13.MKDummy.listeners.OnTabUnselectedListener;
import com.sigmazero13.MKDummy.models.ReferenceCard;
import com.sigmazero13.MKDummy.models.ReferenceCardSection;

public class RefCardFragment extends Fragment implements OnTabUnselectedListener {

	private ReferenceCard card; 
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		if (card == null) {
			card = new ReferenceCard();
			card.setTitle("No Card Selected");
		}
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View mainView = inflater.inflate(R.layout.ref_card_info, container, false);
		
		TextView cardTitle = (TextView)mainView.findViewById(R.id.ref_card_title);
		LinearLayout infoView = (LinearLayout)mainView.findViewById(R.id.ref_card_body);
		
		infoView.removeAllViews();
		
		if (card != null) {
			cardTitle.setText(card.getTitle());
			
			for (ReferenceCardSection section : card.getSections()) {
				View newSection = (LinearLayout)inflater.inflate(R.layout.ref_card_section, container, false);
				TextView sectionTitle = (TextView)newSection.findViewById(R.id.section_title);
				TextView sectionText = (TextView)newSection.findViewById(R.id.section_text);
				sectionTitle.setText(section.getTitle());
				sectionText.setText(section.getText().replace("\\n", System.getProperty("line.separator")));
				infoView.addView(newSection);
			}
		}
		
		return mainView;
	}
	
	@Override
	public void onTabUnselected() {
		// DO NOTHING
	}
	
	
	public void setReferenceCard(ReferenceCard card) {
		this.card = card;
	}

}
